package org.sjs.dreamfly.biz.shortvideo

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import org.sjs.dreamfly.R
import org.sjs.dreamfly.common.base.BaseFragment
import org.sjs.dreamfly.common.util.Router
import org.sjs.dreamfly.common.util.PhemonUtils
import org.sjs.dreamfly.model.VideoInfo


class ShortVideoFragment: BaseFragment() {

    private lateinit var viewModel: ShortVideoViewModel
    private lateinit var recyclerView: RecyclerView

    override fun requireLayoutId(): Int {
        return R.layout.fragment_short_video
    }

    override fun onInitViewModel() {
        viewModel = ViewModelProvider(PhemonUtils.getApplication())[ShortVideoViewModel::class.java]
    }

    override fun onBindViews() {
        recyclerView = requireView().findViewById(R.id.recycler_view)
    }

    override fun onInitViews() {
        recyclerView.adapter = object : RecyclerView.Adapter<VH>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
                return VH(LayoutInflater.from(context).inflate(R.layout.view_holder_short_video_item, null))
            }

            override fun getItemCount(): Int {
                return viewModel.getVideoList().size
            }

            override fun onBindViewHolder(holder: VH, position: Int) {
                holder.bind(viewModel.getVideoList()[position], position)
            }
        }
        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            @SuppressLint("NotifyDataSetChanged")

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                (recyclerView.layoutManager as LinearLayoutManager).run {
                    val lastVisiblePosition = findLastVisibleItemPosition()
                    if (lastVisiblePosition == itemCount - 1) {
                        viewModel.loadMore {
                            recyclerView.adapter?.notifyDataSetChanged()
                        }
                    }
                }
            }
        })
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onLoadData() {
        MainScope().launch {
            viewModel.loadData()
            recyclerView.adapter?.notifyDataSetChanged()
        }
    }
}

class VH(itemView: View) : RecyclerView.ViewHolder(itemView) {
    fun bind(videoInfo: VideoInfo, position: Int) {
        itemView.findViewById<TextView>(R.id.video_name).text = videoInfo.title
        itemView.findViewById<ImageView>(R.id.video_cover).let {
            val radius = 46
            Glide.with(it)
                .load(videoInfo.imageUrl)
                .placeholder(R.color.background)
                .transform(RoundedCornersTransformation(radius, 0,
                    RoundedCornersTransformation.CornerType.TOP))
                .into(it)
        }
        itemView.setOnClickListener {
            Router.launchShortVideoDetail(position)
        }
    }
}